﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace LoginServer
{
    public class ManagerTable
    {
        public Hashtable tables;        
        List<int> TableFree;
        List<int> TableCreated;
        public ManagerTable(GetDB AccessDB, int RoomID)
        {
            tables = new Hashtable();
            Init(AccessDB, RoomID);
        }
        public void Init(GetDB AccessDB, int RoomID)
        {
            TableFree = new List<int>();
            TableCreated = new List<int>();
            for (int i = 0; i < Program.MaxTable;i++ )
            {
                TableFree.Add(i);
            }
            List<TableInfo> listTable = AccessDB.GetAllTableInRoom(RoomID);
            if (listTable != null)
                foreach (var tableinfo in listTable)
                {
                    tableinfo.NickName1 = AccessDB.GetNickName(tableinfo.PlayerID1);
                    tableinfo.NickName2 = AccessDB.GetNickName(tableinfo.PlayerID2);
                    this.AddTable(new Table(tableinfo));
                    Table table = GetTable(tableinfo.TableID);
                }
        }
        public Table GetTable(int key)
        {            
            return (Table)tables[key];
        }
        public void AddTable(Table table)
        {
            tables.Add(table.info.TableID, table);
            TableCreated.Add(table.info.TableID);
            TableFree.Remove(table.info.TableID);
        }
        public void RemoveTable(Table table)
        {
            tables.Remove(table.info.TableID);
            TableCreated.Remove(table.info.TableID);
            TableFree.Add(table.info.TableID);
        }
        public bool AddPlayerInTable(int TableID,int UserID)
        {
            if(CheckTableID(TableID))
            {
                Table table = GetTable(TableID);
                table.info.PlayerID2 = UserID;
                return true;
            }
            return false;
        }

        public bool RemovePlayerInTable(int TableID, int UserID)
        {
            if (CheckTableID(TableID))
            {
                Table table = GetTable(TableID);
                if (table.info.PlayerID2 == UserID)
                    table.info.PlayerID2 = -1;
                else
                    table.info.PlayerID1 = -1;
                return true;
            }
            return false;
        }

        public bool CheckEmptyPlayerInTable(int TableID)
        {
            if (CheckTableID(TableID))
            {
                Table table = GetTable(TableID);
                if (table.info.PlayerID1 == -1 && table.info.PlayerID2 == -1)
                    return true;
                else
                    return false;
            }
            return false;
        }
        
        
        public int GetNewID()
        {
            if(TableFree.Count > 0)
                return TableFree[0];
            return -1;
        }        
        public bool CheckTableID(int ID)
        {
            if (tables.ContainsKey(ID))
                return true;
            Console.WriteLine("table not exits");
            return false;
        }
        internal int Check_Player_Table(int ID)
        {
            Table t = (Table)tables[ID];
            if (t.info.PlayerID2 == -1 && t.info.PlayerID1 != -1)
            {
                return 1;
            }
            else if (t.info.PlayerID1 == -1)
            {
                Console.WriteLine("not user in table");
                return -2;
            }
            else
            {
                Console.WriteLine("full user in table");
                return -1;
            }
        }
    }
}
